package com.yuchs.test.log4j;

import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender;
import org.apache.logging.log4j.core.appender.db.nosql.NoSQLProvider;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttr;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;


@Plugin(name = "PlayerLogAppender", category = "Core", elementType = "appender", printObject = true)
public final class PlayerLogAppender extends AbstractDatabaseAppender<PlayerLogDatabaseManager>{

	private PlayerLogAppender(String name, Filter filter,
			boolean handleException, PlayerLogDatabaseManager manager){
		
		super(name, filter, handleException, manager);
	}
	
	@PluginFactory
	public static PlayerLogAppender createAppender(@PluginAttr("name") String name,
													@PluginAttr("suppressExceptions") String suppress,
													@PluginElement("filters") Filter filter,
													@PluginAttr("bufferSize") final String bufferSize,
		                                            @PluginElement("noSqlProvider") final NoSQLProvider<?> provider){
		
		boolean suppressExceptions = suppress == null ? false : Boolean.valueOf(suppress);
		
		if(name == null){
			throw new IllegalArgumentException("No name provider for " + PlayerLogAppender.class.getSimpleName());
		}
		
		int buffSize = AbstractAppender.parseInt(bufferSize, 0);
		
		final PlayerLogDatabaseManager manager = PlayerLogDatabaseManager.getNoSQLDatabaseManager(name,buffSize,provider);
		if(manager == null){
			throw new IllegalArgumentException("PlayerLogDatabaseManager.getNoSQLDatabaseManager() reutnr null");
		}
		
		return new PlayerLogAppender(name, filter, suppressExceptions,manager);
	}
}
